package com.wf.course.service.impl;
import java.io.IOException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.utils.page.PageBean;
import com.wf.course.dao.IGoodsParamMapper;
import com.wf.course.model.ad.AdInfo;
import com.wf.course.model.goods.GoodsParam;
import com.wf.course.service.IGoodsParamService;
import com.wf.course.web.Constants;
import com.wf.course.web.OSSUtil;

@Service
@Transactional(rollbackFor=Exception.class)
public class IGoodsParamServiceImpl implements IGoodsParamService{
	
	@Autowired
	private IGoodsParamMapper mapper;

	@Override
	public void indexInfo(PageBean<GoodsParam> pageBean) {
		// TODO Auto-generated method stub
		pageBean.setResults(this.mapper.queryList(pageBean));
		pageBean.setTotalCount(this.mapper.getTotalCount(pageBean).intValue());
	}

	@Override
	public void saveOrUpdateGoodsParam(GoodsParam goodsParam,MultipartFile paramImg) {
		// TODO Auto-generated method stub
		if(goodsParam.getId()==null){
			if (!paramImg.isEmpty()) {
				String imgUrl = "";
				try {
					imgUrl = OSSUtil.addFile(paramImg, "paramImg", Constants.IGM_BUCKET);
				} catch (IOException e) {
					e.printStackTrace();
				}
			
				goodsParam.setParamImg(imgUrl);
				
			}
			
			goodsParam.setParamName("规格");
			this.mapper.saveGoodsParam(goodsParam);
		}else{
			
			if (paramImg.getSize() != 0) {
				GoodsParam gdsParam = mapper.queryGoodsParamById(goodsParam.getId());
				try{
					if(StringUtils.isNotBlank(gdsParam.getParamImg())){
						// 删除原来图片
						OSSUtil.delete(gdsParam.getParamImg(), Constants.IGM_BUCKET);
					}
				}catch(Exception e){
					System.out.println(e.getMessage());
				}
				// 重新上传
				// 上传
				String imgUrl = "";
				try {
					imgUrl = OSSUtil.addFile(paramImg, "paramImg", Constants.IGM_BUCKET);
				} catch (IOException e) {
					e.printStackTrace();
				}
				goodsParam.setParamImg(imgUrl);
			}
			this.mapper.updateGoodsParam(goodsParam);
		}
		
	}

	@Override
	public GoodsParam queryGoodsParamById(Integer id) {
		// TODO Auto-generated method stub
		return this.mapper.queryGoodsParamById(id);
	}

	@Override
	public void deleteById(Integer id) {
		// TODO Auto-generated method stub
		this.mapper.deleteById(id);
	}

	@Override
	public void deleteByGoodsId(Integer goodsId) {
		// TODO Auto-generated method stub
		this.mapper.deleteByGoodsId(goodsId);
		
	}
	@Override
	public List<GoodsParam> queryGoodsParamByCondition(Integer goodsId,Integer showFlag){
		return this.mapper.queryGoodsParamByCondition(goodsId,showFlag);
	}
	
}
